import { RoleEntity } from 'src/api/role/entities/role.entity';
import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { ResourceEntity } from './resource.entity';

@Entity('resource_role')
// 中间表： 多对一，其他对应的中间表：一对多
// 关联角色和资源的表
export class ResourceRoleEntity extends SharedEntity {
  // 关联角色的id
  @Column({
    type: 'varchar',
    length: 36,
    name: 'role_id',
    comment: '关联角色的id',
  })
  roleId: string;

  @ManyToOne(() => RoleEntity, (role) => role.roleResource, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'role_id', referencedColumnName: 'id' }])
  role: RoleEntity;

  // 关联的资源id
  @Column({
    type: 'varchar',
    length: 36,
    name: 'resource_id',
    comment: '关联的资源id',
  })
  resourceId: string;

  @ManyToOne(() => ResourceEntity, (resource) => resource.roleResource)
  @JoinColumn([{ name: 'resource_id', referencedColumnName: 'id' }])
  resource: ResourceEntity;
}
